package Array;

import java.util.Arrays;

public class MyArrayList implements ArrayInter{
    private int[] arr=null;
    private int head=0;
    private int size=0;
    public MyArrayList(int capacity){
        arr=new int[capacity];
    }
    @Override
    public void add(int data) {
        //默认尾插
        if(size==arr.length){
            isFUll();
        }
        arr[size++]=data;
    }
    public void isFUll(){
        this.arr= Arrays.copyOf(this.arr,this.arr.length*2);
    }
    @Override
    public void add(int pos, int data) {
        if(pos<0||pos>=arr.length){
            System.out.println("插入的位置不合法！");
            return;
        }else{
            if (size==arr.length) {
                isFUll();
            }
            for(int i=size;i>pos;i--){
                arr[i]=arr[i-1];
            }
            arr[pos]=data;
            size++;
        }
    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < size; i++) {
            if(arr[i]==toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < size; i++) {
            if(arr[i]==toFind){
                return i;
            }
        }
        System.out.println("未找到该元素");
        return -1;
    }

    @Override
    public int get(int pos) {
        if(pos<0||pos>=size){
            System.out.println("pos的位置不合法！");
            return -1;
        }
       return arr[pos];
    }

    @Override
    public void set(int pos, int value) {
        if(pos<0||pos>=size){
            System.out.println("pos的位置不合法！");
            return;
        }
        arr[pos]=value;
    }

    @Override
    public void remove(int toRemove) {
        //不在管理细节
        for(int i=toRemove;i<=size;i++){
            arr[i]=arr[i+1];
        }
        size--;
    }

    @Override
    public int size() {
        return this.size;
    }

    @Override
    public void clear() {
        size=0;
    }

    @Override
    public void display() {
        for(int i=0;i<size;i++){
            System.out.print(arr[i]+" ");
        }
        System.out.println();
    }
}
